package com.spider.admin.scaffold.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spider.admin.scaffold.service.FeedbackService;
import com.spider.admin.scaffold.utils.DateUtils;
import com.spider.admin.scaffold.entity.enums.DataDictionaryEnum;
import com.spider.admin.scaffold.entity.model.Feedback;
import com.spider.admin.scaffold.entity.param.BaseQueryParam;
import com.spider.admin.scaffold.exception.BusinessException;
import com.spider.admin.scaffold.exception.ExceptionDef;
import com.spider.admin.scaffold.mapper.FeedbackMapper;
import org.springframework.stereotype.Service;

/**
 * @author xiong.bo
 * @version 1.0
 * @date 2021/10/5 11:01 上午
 */

@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper,Feedback> implements FeedbackService {


    @Override
    public Page<Feedback> selectPage(BaseQueryParam queryParam) {
        LambdaQueryWrapper<Feedback> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Feedback::getCreateTime);
        return this.baseMapper.selectPage(queryParam.buildPage(), queryWrapper);
    }

    @Override
    public Feedback saveFeedback(Feedback feedback) {
        feedback.setStatus(DataDictionaryEnum.UNDISPOSED.getCode());
        this.baseMapper.insert(feedback);
        return feedback;
    }

    @Override
    public Feedback handleFeedback(Feedback feedback) {
        if (feedback.getId() == null || feedback.getDispose() == null) {
            throw new BusinessException(ExceptionDef.ERROR_COMMON_PARAM_NULL);
        }
        Feedback dataFeedback = this.baseMapper.selectById(feedback.getId());
        if (dataFeedback==null) {
            throw new BusinessException(ExceptionDef.ERROR_DATA_NOT_EXIST);
        }
        dataFeedback.setDispose(feedback.getDispose());
        dataFeedback.setStatus(DataDictionaryEnum.DISPOSED.getCode());
        this.baseMapper.updateById(dataFeedback);
        return feedback;
    }
}
